package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.OrderModelCalculateHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.DiscountRateAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liaojing
 * 雁飞格物DMP平台-公有化 处理订购模式选择联动后的总结算价和总销售价
 * @create 2021-12-31-15:24
 */
@Service
@AttrHandler(DiscountRateAttrHandler.class)
public class DiscountRateAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {

        //订购模式
        String OrderModel = inst.getAttr("210008072").getValue();
        //N1档次-（0-1000]/D1结算价格（万元）
//        KiteAttr LastestPriceAttr_N1 = inst.getAttr("210008080");
        //N1档次-（0-1000]/D1销售价格（万元）
        KiteAttr sellAttr_N1 = inst.getAttr("210008081");
        //N2档次-（1000-2000]/D2结算价格（万元）
//        KiteAttr LastestPriceAttr_N2 = inst.getAttr("210008082");
        //N2档次-（1000-2000]/D2销售价格（万元）
        KiteAttr sellAttr_N2 = inst.getAttr("210008083");
        //N3档次-（2000-3000]/D3结算价格（万元）
//        KiteAttr LastestPriceAttr_N3 = inst.getAttr("210008084");
        //N3档次-（2000-3000]/D3销售价格（万元）
        KiteAttr sellAttr_N3 = inst.getAttr("210008085");
        //订购年限
        KiteAttr year = inst.getAttr("210008134");

        //获取N1结算价格、销售价格
//        String lpa_n1 = LastestPriceAttr_N1.getValue();
        String sa_n1 = sellAttr_N1.getValue();
        //获取N2结算价格、销售价格
//        String lpa_n2 = LastestPriceAttr_N2.getValue();
        String sa_n2 = sellAttr_N2.getValue();
        //获取N3结算价格、销售价格
//        String lpa_n3 = LastestPriceAttr_N3.getValue();
        String sa_n3 = sellAttr_N3.getValue();
//        //结算价折扣率
//        String discountRate = inst.getAttr("210008073").getValue();
        //订购年限
        String orderYear = year.getValueDesc();

        //订购模式为包年服务N1档次
        if(KiteStringUtils.isEqual(OrderModel, "110015093")){
//            //总结算价计算值
//            if(!KiteStringUtils.isEmpty(lpa_n1) && !KiteStringUtils.isEmpty(orderYear)){
//                String result_lpa_n1 = OrderModelCalculateHelper.getResultLpa(lpa_n1,discountRate,orderYear);
//                if(KiteStringUtils.isNotEmpty(result_lpa_n1)) {
//                    operateInstAttrService.setAttrValue(
//                            inst, "210008139", result_lpa_n1, data.getSceneInstId(), null);
//                }
//            }
            //总销售价计算值
            if(!KiteStringUtils.isEmpty(sa_n1) && !KiteStringUtils.isEmpty(orderYear)){
                String result_sa_n1 = OrderModelCalculateHelper.getResultSa(sa_n1,orderYear);
                if(KiteStringUtils.isNotEmpty(result_sa_n1)) {
                    operateInstAttrService.setAttrValue(
                            inst, "210008140", result_sa_n1, data.getSceneInstId(), null);
                }
            }
        }
        //订购模式为包年服务N2档次
        if(KiteStringUtils.isEqual(OrderModel, "110015094")){
//            //总结算价计算值
//            if(!KiteStringUtils.isEmpty(lpa_n2)  && !KiteStringUtils.isEmpty(orderYear)){
//                String result_lpa_n2 = OrderModelCalculateHelper.getResultLpa(lpa_n2,discountRate,orderYear);
//                if(KiteStringUtils.isNotEmpty(result_lpa_n2)) {
//                    operateInstAttrService.setAttrValue(
//                            inst, "210008139", result_lpa_n2, data.getSceneInstId(), null);
//                }
//            }
            //总销售价计算值
            if(!KiteStringUtils.isEmpty(sa_n2)  && !KiteStringUtils.isEmpty(orderYear)){
                String result_sa_n2 = OrderModelCalculateHelper.getResultSa(sa_n2,orderYear);
                if(KiteStringUtils.isNotEmpty(result_sa_n2)) {
                    operateInstAttrService.setAttrValue(
                            inst, "210008140", result_sa_n2, data.getSceneInstId(), null);
                }
            }
        }
        //订购模式为包年服务N3档次
        if(KiteStringUtils.isEqual(OrderModel, "110015095")){
//            //总结算价计算值
//            if(!KiteStringUtils.isEmpty(lpa_n3) && !KiteStringUtils.isEmpty(orderYear)){
//                String result_lpa_n3 = OrderModelCalculateHelper.getResultLpa(lpa_n3,discountRate,orderYear);
//                if(KiteStringUtils.isNotEmpty(result_lpa_n3)) {
//                    operateInstAttrService.setAttrValue(
//                            inst, "210008139", result_lpa_n3, data.getSceneInstId(), null);
//                }
//            }
            //总销售价计算值
            if(!KiteStringUtils.isEmpty(sa_n3) && !KiteStringUtils.isEmpty(orderYear)){
                String result_sa_n3 = OrderModelCalculateHelper.getResultSa(sa_n3,orderYear);
                if(KiteStringUtils.isNotEmpty(result_sa_n3)) {
                    operateInstAttrService.setAttrValue(
                            inst, "210008140", result_sa_n3, data.getSceneInstId(), null);
                }
            }
        }

        // 处理结算价
        OrderModelCalculateHelper.calculateSettlePrice(attr, inst ,data);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
